package com.java.product.controller;

import com.java.param.AdminProductParam;
import com.java.param.ProductIdRemoveParam;
import com.java.pojo.Product;
import com.java.product.service.ProductService;
import com.java.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/product")
public class ProductAdminController {
    @Autowired
    private ProductService productService;
    @PostMapping("/admin/save")
    public R adminSave(@RequestBody AdminProductParam adminProductParam){
        return productService.adminSave(adminProductParam);
    }

    @PostMapping("/admin/update")
    public R adminUpdate(@RequestBody Product product){
        return productService.adminUpdate(product);
    }

    @PostMapping("/admin/remove")
    public R adminRemove(@RequestBody @Validated ProductIdRemoveParam productIdRemoveParam, BindingResult result){
        if(result.hasErrors()){
            return R.fail("商品为空，不可删除！");
        }
        return productService.adminRemove(productIdRemoveParam);
    }
}
